﻿using PlayListSet.Comm;
using SqlSugar;
using SqlSugar.DbConvert;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace PlayListSet.Entity
{
    public class UserPlayInfoCmd:System.ComponentModel.INotifyPropertyChanged
    {
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public UserCommand Cmd { get; set; }
        public string ID {  get; set; }
    
        public UserPlayInfoCmd() { ID = AppHelp.GetGuid(); Cmd = new UserCommand(); }
        private int row;
        public int Row
        {
            set { row = value; PropertyChangedEvent(nameof(Row)); }
            get { return row; }
        }
        private string name ="节目1";
        public string Name
        {
            set { name = value; PropertyChangedEvent(nameof(Row)); }
            get { return name; }
        }

        //--------------------11111
        private int cmd1Type=0;
        /// <summary>
        /// 0 无 1 播放 2 停止 3 暂停 4 跳转 5 音量
        /// </summary>
        public int Cmd1Type
        {
            set { cmd1Type = value; PropertyChangedEvent(nameof(Cmd1Type)); }
            get=>cmd1Type;
        }
        private string cmd1Arg = string.Empty;
        /// <summary>
        /// 参数
        /// </summary>
        public string Cmd1Arg
        {
            set { cmd1Arg = value; PropertyChangedEvent(nameof(Cmd1Arg)); }
            get => cmd1Arg;
        }
        private int cmd1Vol = 10;
        /// <summary>
        /// 音量
        /// </summary>
        public int Cmd1Vol
        {
            set { cmd1Vol = value; PropertyChangedEvent(nameof(Cmd1Vol)); }
            get => cmd1Vol;
        }
        private double dbTimeSpan1;
        public double DbTimeSpan1
        {
            set { dbTimeSpan1 = value; PropertyChangedEvent(nameof(DbTimeSpan1)); }
            get => dbTimeSpan1;
        }
        /// <summary>
        /// 时间1
        /// </summary>
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public TimeSpan TimeSpan1
        {
            set { 
                if(value == TimeSpan.Zero)
                {
                    DbTimeSpan1 = 0;
                }
                else
                {
                    DbTimeSpan1 = value.TotalSeconds;
                }
                
                PropertyChangedEvent(nameof(TimeSpan1)); }
            get => TimeSpan.FromSeconds(DbTimeSpan1);
        }

        //-----------------------2222222222






        private int cmd2Type = 0;
        /// <summary>
        /// 0 无 1 播放 2 停止 3 暂停 4 跳转 5 音量 6 自定义
        /// </summary>
        public int Cmd2Type
        {
            set { cmd2Type = value; PropertyChangedEvent(nameof(Cmd2Type)); }
            get => cmd2Type;
        }
        private string cmd2Arg = string.Empty;
        /// <summary>
        /// 参数
        /// </summary>
        public string Cmd2Arg
        {
            set { cmd2Arg = value; PropertyChangedEvent(nameof(Cmd2Arg)); }
            get => cmd2Arg;
        }
        private int cmd2Vol = 10;
        /// <summary>
        /// 音量
        /// </summary>
        public int Cmd2Vol
        {
            set { cmd2Vol = value; PropertyChangedEvent(nameof(Cmd2Vol)); }
            get => cmd2Vol;
        }
        private double dbTimeSpan2;
        public double DbTimeSpan2
        {
            set { dbTimeSpan2 = value; PropertyChangedEvent(nameof(DbTimeSpan2)); }
            get => dbTimeSpan2;
        }
        /// <summary>
        /// 时间2
        /// </summary>
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public TimeSpan TimeSpan2
        {
            set
            {
                if (value == TimeSpan.Zero)
                {
                    DbTimeSpan2 = 0;
                }
                else
                {
                    DbTimeSpan2 = value.TotalSeconds;
                }

                PropertyChangedEvent(nameof(TimeSpan2));
            }
            get => TimeSpan.FromSeconds(DbTimeSpan2);
        }




        //--------------------333333333333333
        private int cmd3Type = 0;
        /// <summary>
        /// 0 无 1 播放 2 停止 3 暂停 4 跳转 5 音量
        /// </summary>
        public int Cmd3Type
        {
            set { cmd3Type = value; PropertyChangedEvent(nameof(Cmd3Type)); }
            get => cmd3Type;
        }
        private string cmd3Arg = string.Empty;
        /// <summary>
        /// 参数
        /// </summary>
        public string Cmd3Arg
        {
            set { cmd3Arg = value; PropertyChangedEvent(nameof(Cmd3Arg)); }
            get => cmd3Arg;
        }
        private int cmd3Vol = 10;
        /// <summary>
        /// 音量
        /// </summary>
        public int Cmd3Vol
        {
            set { cmd3Vol = value; PropertyChangedEvent(nameof(Cmd3Vol)); }
            get => cmd3Vol;
        }
        private double dbTimeSpan3;
        public double DbTimeSpan3
        {
            set { dbTimeSpan3 = value; PropertyChangedEvent(nameof(DbTimeSpan3)); }
            get => dbTimeSpan3;
        }
        /// <summary>
        /// 时间2
        /// </summary>
        [SqlSugar.SugarColumn(IsIgnore = true)]
        public TimeSpan TimeSpan3
        {
            set
            {
                if (value == TimeSpan.Zero)
                {
                    DbTimeSpan3 = 0;
                }
                else
                {
                    DbTimeSpan3 = value.TotalSeconds;
                }

                PropertyChangedEvent(nameof(TimeSpan3));
            }
            get => TimeSpan.FromSeconds(DbTimeSpan3);
        }
        public static void Save(List<UserPlayInfoCmd> infos)
        {
            //var json = Newtonsoft.Json.JsonConvert.SerializeObject(infos, Formatting.Indented);
            //System.IO.File.WriteAllText(
            //    System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "cfg", "PlayInfos.json"),
            //    json, Encoding.UTF8);
            var db = AppHelp.GetAppSqlSugarClient();
            db.Deleteable<UserPlayInfoCmd>().ExecuteCommand();
            db.Insertable<UserPlayInfoCmd>(infos).ExecuteCommand();
        }
        public static List<UserPlayInfoCmd> Read()
        {
            //var text = System.IO.File.ReadAllText(
            //     System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "cfg", "PlayInfos.json"), Encoding.UTF8);
            //return Newtonsoft.Json.JsonConvert.DeserializeObject<List<PlayInfo>>(text);
            var cfg = AppHelp.GetAppSqlSugarClient().Queryable<UserPlayInfoCmd>().ToList();
            if (cfg == null)
                return new List<UserPlayInfoCmd>();
            return cfg;
        }
        public static List<UserPlayInfoCmd> CopyTo(IList<UserPlayInfoCmd> userPlayInfoCmds)
        {
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(userPlayInfoCmds);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<List<UserPlayInfoCmd>>(json);
        }
        public void PropertyChangedEvent(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
         public event PropertyChangedEventHandler PropertyChanged;
        [SqlSugar.SugarColumn(IsIgnore =true)]
        public List<string> CmdTypes
        {
            get
            {
                // 0 无 1 播放 2 停止 3 暂停 4 跳转 5 音量 6 自定义
                return new List<string>() { "无操作","播放","停止","暂停","跳转","音量","自定义"};
            }
        }

        public string GetCmd()
        {
            StringBuilder sb = new StringBuilder("");
            if (Cmd1Type == 1)
            {
                sb.Append("play:;");
            }
            else if (Cmd1Type == 2) 
            {
                sb.Append("stop:;");
            }
            else if (Cmd1Type == 3)
            {
                sb.Append("pause:;");
            }
            else if (Cmd1Type == 4)
            {
                sb.Append($"seek:{TimeSpan1.ToString()};");
            }
            else if (Cmd1Type == 5)
            {
                sb.Append($"vol:{Cmd1Vol};");
            }
            else if (Cmd1Type == 6)
            {
                sb.Append(Cmd1Arg.TrimStart(';').TrimEnd(';')+";");
            }

            var arg1 = sb.ToString();
            sb = new StringBuilder(arg1.Trim(';')+";");
            if (Cmd2Type == 1)
            {
                sb.Append("play:;");
            }
            else if (Cmd2Type == 2)
            {
                sb.Append("stop:;");
            }
            else if (Cmd2Type == 3)
            {
                sb.Append("pause:;");
            }
            else if (Cmd2Type == 4)
            {
                sb.Append($"seek:{TimeSpan2.ToString()};");
            }
            else if (Cmd2Type == 5)
            {
                sb.Append($"vol:{Cmd2Vol};");
            }
            else if (Cmd2Type == 6)
            {
                sb.Append(Cmd2Arg.TrimStart(';').TrimEnd(';') + ";");
            }

            arg1 = sb.ToString();
            sb = new StringBuilder(arg1.Trim(';') + ";");
            if (Cmd3Type == 1)
            {
                sb.Append("play:;");
            }
            else if (Cmd3Type == 2)
            {
                sb.Append("stop:;");
            }
            else if (Cmd3Type == 3)
            {
                sb.Append("pause:;");
            }
            else if (Cmd3Type == 4)
            {
                sb.Append($"seek:{TimeSpan3.ToString()};");
            }
            else if (Cmd3Type == 5)
            {
                sb.Append($"vol:{Cmd3Vol};");
            }
            else if (Cmd3Type == 6)
            {
                sb.Append(Cmd3Arg.TrimStart(';').TrimEnd(';') + ";");
            }

            arg1 = sb.ToString();
            return arg1.Trim(';');
        }
    }
    
}
